home *** CD-ROM | disk | FTP | other *** search
- Path: HOPPER.ACM.ORG!news
- From: varnk@e62.diebold.com (Ken Varn)
- Newsgroups: comp.lang.c
- Subject: Re: Formatted print with variable output field width.
- Date: 10 Apr 1996 12:42:51 GMT
- Organization: Diebold
- Message-ID: <4kgacb$r3s@HOPPER.ACM.ORG>
- References: <berlinerDpMBoE.AEn@netcom.com>
- NNTP-Posting-Host: 199.218.232.47
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <berlinerDpMBoE.AEn@netcom.com>, berliner@netcom.com says...
- />
- />Does anyone know of a way of creating a variable output field width?
- />Say, for example, I wish to have a width of v+3, where v is some
- />integer, the result of some calculation. Is there some way to say
- />something like
- />
- /> printf("%<v+3>.<v>f", floatingnum);
- />
- />where the result of the evaluation of v+3 and of v will be
- />fed to printf?
-
- All of the printf family of functions allow you to send arguments to format
- specifiers using the asterisk (*). For example if you wanted to format a
- float to have variable decimal places and width, then use the expression
-
- width = 5;
- precision = 3;
- printf("%*.*f",width,precision,floatingnum);
-
- Note that when using numerical output instructions, the width specifier is
- only a minimum width. If the floating point number exceeds the width, it
- will print the number in its entirety. This could potentially be dangerous
- if using sprintf.
-
-